'''
Created on 2010-02-18

@author: tomek
'''

import gtk
import gobject

class outDir(gtk.Window):
    def __init__(self, pyscreenshot):
        
        super(outDir, self).__init__()
        self.set_modal(True)
        self.set_title("Output dirs")
        self.set_size_request(350, -1)
        self.set_position(gtk.WIN_POS_CENTER)
        
        #self.app_config_dir = pyscreenshot.app_config_dir
        
        self.pyscreenshot = pyscreenshot
        
        box = gtk.VBox()
        self.add(box)
        
        self.entryPath = gtk.Entry()
        
        hbox = gtk.HBox()
        
        buttonAdd = gtk.Button("Add")
        buttonAdd.connect("clicked", self.button_click_add)
        hbox.pack_start(buttonAdd,  True, True, 0)
        
        buttonRemove = gtk.Button("Remove")
        buttonRemove.connect("clicked", self.button_click_remove)
        hbox.pack_start(buttonRemove,  True, True, 0)
        
        buttonSave = gtk.Button("Save")
        buttonSave.connect("clicked", self.button_click_save)
        hbox.pack_start(buttonSave,  True, True, 0)
        
        
        box.pack_start(self.entryPath, False, False, 0)
        box.pack_start(hbox, False, False, 0)
        box.pack_start(self.show_list(), True, True, 0)
        
        
        self.get_file()
        
        self.show_all()
        
        
    def show_list(self):
        scrolled_window = gtk.ScrolledWindow()
        scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.model = gtk.ListStore(gobject.TYPE_STRING)
        self.tree_view = gtk.TreeView(self.model)
        self.tree_view.set_events(gtk.gdk.BUTTON_PRESS_MASK)
    
        scrolled_window.add_with_viewport (self.tree_view)
        self.tree_view.show()
        
        cell = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Output dirs", cell, text=0)
        self.tree_view.append_column(column)
        
        return scrolled_window


    def get_file(self):
        try:
            o = open( self.pyscreenshot.app_config_dir + '/outdir.list' , "r")
        except IOError:
            o=False
        if o:    
            line = o.readlines()
            for l in line:
                print l.replace( "\n", "" )
                iter = self.model.append()
                self.model.set(iter, 0, l.replace( "\n", "" ))
            o.close()
        
    def button_click_add(self, widget):
        newO = self.entryPath.get_text()
        print len(newO)
        if len(newO)>0:
            iter = self.model.append()
            self.model.set(iter, 0, newO)
        else:
            md = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, "Output dir is empty!")
            md.run()
            md.destroy()
            
        
    def button_click_remove(self, widget):
        selection = self.tree_view.get_selection()
        (model, iter) = selection.get_selected()
        if iter:
            model.remove(iter)
        
    def button_click_save(self, widget):
        pass
        str=""
        for row in self.model:
            print row[0]
            str += row[0] + "\n"
        print self.pyscreenshot.app_config_dir + '/outdir.list'
        o = open( self.pyscreenshot.app_config_dir + '/outdir.list' , "w")
        o.write(str);
        o.close()
        self.pyscreenshot.combobox1Out.set_model( self.model )
        self.pyscreenshot.combobox1Out.set_text_column(0)
        
            
        
        

